<?php

class Connection
{
	var $host;
	var $user;
	var $password;
	var $database;
	var $conn;
	
	function Connection()
	{

	$this->host = DB_HOST;
	$this->user = DB_USER;
	$this->password = DB_PASSWORD;
	$this->database = DB_DATABASE;
	$this->connectToServer();
	$this->selectDatabase();
	}
	
	function connectToServer()
	{
	$this->conn = mysql_connect ($this->host, $this->user, $this->password ) or die(mysql_error());
	$this->selectDatabase();
	}
	
	function selectDatabase()
	{
	mysql_select_db ( $this->database , $this->conn )  or die(mysql_error());
	}
	
	function selectQuery($query)
	{
	
	if ( ! $this->conn ) {
		echo ("No database connection");
	}
	
	$result = mysql_query ( $query, $this->conn )  or die (mysql_error() );
	return $result;
	}
	
	function returnArray($result){
	/*if ( ! $result = $this->selectQuery($query) ) {
		return false;
	}*/
	
	$ret = array();
	
	while ( $row = mysql_fetch_assoc( $result ) ) {
		$ret[] = $row;
		}
	return $ret;
	
	}
	
	function updateSector( $sectorName, $sectorId){
	
		$sql = "UPDATE `sectors` SET `nameOfSector` = '".$sectorName."' WHERE `sectors`.`id` = ".$sectorId." LIMIT 1 ;";
 		$result = $this->selectQuery($sql);
 		
 		if ($result){
 		header("Location:index.php?act=sections");
 		}
	}
	
	function addSector( $sectorName){
	
		$sql = "INSERT INTO `sectors` VALUES ('', '".$sectorName."');";
 		$result = $this->selectQuery($sql);
 		
 		if ($result){
 		header("Location:index.php?act=sections");
 		}
	}
	
	function deleteSector( $id ){
	
		$sql = "DELETE FROM `sectors` WHERE `id` = ".$id.";";
 		$result = $this->selectQuery($sql);
 		
 		if ($result){
 		header("Location:index.php?act=sections");
 		}
	}
	
	
}
	
?>